import 'babel-polyfill'
import Vue from 'vue'
import App from './App.vue'
import router from './router/index'
import store from './store/index'
import "amfe-flexible" // 引入flexible
import Vant from 'vant';
import './components'
import 'vant/lib/index.css';
import './assets/iconfont/iconfont.css'
import './assets/iconfont/iconfont.js'
import './assets/css/base.scss'
import {
	Lazyload
} from 'vant';
import {
	Dialog
} from 'vant';
import {
	Toast
} from 'vant';
import {
	setLocalStorage,
	getLocalStorage
} from '@/utils/auth';
import {
	getToken,
	getMemberInfo,
	getTheme
} from '@/api/global.js'

Vue.use(Vant);
Vue.use(Lazyload);
Vue.use(Dialog);
Vue.use(Toast);
//设置为 false 以阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
router.beforeEach((to, from, next) => {
	const token = getLocalStorage('token')
	const openId = getLocalStorage('openId')
	console.log(token, openId)
	if (to.meta.title) {
		document.title = to.meta.title
	}
	if (!openId && !token) {
		Toast.setDefaultOptions({
			duration: 0
		});
		Toast.loading({
			message: '登陆中...',
			forbidClick: true
		});
		let appid = '';
		let organiseId=''
		let redirect_uri = window.location.href;
		redirect_uri = redirect_uri.split("/authorization")[0]
		let code = getUrlCode().code; // 截取code
		appid = getQueryVariable('appid'); // appid
		organiseId = getQueryVariable('organiseId'); // 截取code
		console.log(code, organiseId, appid);
		if (!code) {
			// 如果没有code，则去请求
			window.location.href =
				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}?organiseId=${organiseId}&appid=${appid}&response_type=code&scope=snsapi_base&state=&connect_redirect=1#wechat_redirect`;
		} else {
			getToken({
					organiseId: organiseId
				})
				.then(res => {
					setLocalStorage('token', res.data.access_token);
					getMemberInfo({
							appid: appid,
							code: code
						})
						.then(res => {
							if (res.success) {
								setLocalStorage('openId', res.data.openId);
								store.commit('setUser', res.data);
								setLocalStorage('userData', res.data);
								getTheme({
										organiseId: organiseId
									})
									.then(res => {
										if (res.success) {


										} else {
											this.$toast(res.message);
										}
									});
								  Toast.clear();
								router.push({
									path: getLocalStorage('now_url')
								});
							} else {
								window.location.href =
									`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}?organiseId=${organiseId}&appid=${appid}&response_type=code&scope=snsapi_base&state=&connect_redirect=1#wechat_redirect`;
							}
						});
				});
		}
	} else {
		next()
	}
})

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}


function getUrlCode() {
	// 截取url中的code方法
	var url = location.search;
	var theRequest = new Object();
	if (url.indexOf('?') != -1) {
		var str = url.substr(1);
		var strs = str.split('&');
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
		}
	}
	return theRequest;
}

export default new Vue({
	router,
	store,
	render: h => h(App),
}).$mount('#app')
